package com.gmail.yesguxun.flee;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.stackmob.android.sdk.common.StackMobCommon;
import com.stackmob.sdk.api.StackMob;
import com.stackmob.sdk.api.StackMobQuery;
import com.stackmob.sdk.callback.StackMobCallback;
import com.stackmob.sdk.exception.StackMobException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gmail.yesguxun.flee.model.*;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

public class FleeMarketActivity extends Activity {
    private StackMob mStackMob;
    
    //https://www.stackmob.com/platform/stackmob/help/topics/Getting-Started:-Android-SDK
    //https://www.stackmob.com/platform/stackmob/help/topics/Getting-Started:-Java-Client-SDK
    //java sdk said we do not need to set up user object
    //where to set  ? like IOS SDK?
    //#define STACKMOB_APP_SUBDOMAIN      @"YOUR SUBDOMAIN"
    //#define STACKMOB_APP_DOMAIN         @"stackmob.com" 
    
    private Button mButton1 = null;
    private TextView mTextView1 = null;
    Gson mGson = new Gson();
    
    class ClickHandler implements View.OnClickListener
    {
        public void onClick(View v) {
            
            //test1Get();
            test2Query();
        }
        
        private void test1Get()
        {
            Map<String, String> arguments = new HashMap<String, String>();
            Log.i("StackMob", "test1Get threadid:" + Thread.currentThread().getId());
            mStackMob.get("flee", arguments, new StackMobCallback() {
                
                String result = null;
                
                @Override
                public void success(String responseBody) {
                    result = "got object with response: " + responseBody;
                    Log.i("StackMob", result);
                    Log.i("StackMob", "success threadid:" + Thread.currentThread().getId());
                  //responseBody will be a list of MyObject instances
                    Type myObjectListType = new TypeToken<List<Flee>>() {}.getType();
                    List<Flee> objects = mGson.fromJson(responseBody, myObjectListType);
                    threadAgnosticInfo(mTextView1, result + " " + objects.get(0).name + " " + objects.get(0).address1);
                }
                
                @Override
                public void failure(StackMobException e) {
                    result = "failed to get object with exception: " + e.toString();
                    Log.i("StackMob", result);
                    Log.i("StackMob", "failure threadid:" + Thread.currentThread().getId());
                    threadAgnosticInfo(mTextView1, result);
                }
                
            });
        }
        
        //https://www.stackmob.com/platform/stackmob/help/topics/Getting-Started:-Java-Client-SDK#a-advanced_queries
        private void test2Query()
        {
            StackMobQuery q = new StackMobQuery("flee").field("time_start").isGreaterThan("197212201220").getQuery();
            Log.i("StackMob", "test2Query threadid:" + Thread.currentThread().getId());
            
            mStackMob.get(q, new StackMobCallback() {
                
                String result = null;
                
                @Override
                public void success(String responseBody) {
                    result = "got object with response: " + responseBody;
                    Log.i("StackMob", result);
                    Log.i("StackMob", "success threadid:" + Thread.currentThread().getId());
                    //responseBody will be a list of MyObject instances
                    Type myObjectListType = new TypeToken<List<Flee>>() {}.getType();
                    List<Flee> objects = mGson.fromJson(responseBody, myObjectListType);
                    threadAgnosticInfo(mTextView1, result + " " + objects.get(0).name + " " + objects.get(0).address1);
                }
                
                @Override
                public void failure(StackMobException e) {
                    result = "failed to get object with exception: " + e.toString();
                    Log.i("StackMob", result);
                    Log.i("StackMob", "failure threadid:" + Thread.currentThread().getId());
                    threadAgnosticInfo(mTextView1, result);
                }
                
            });
        }
    }
    
    public FleeMarketActivity() {
        //key information https://www.stackmob.com/platform/account/apps/reset
        //about set up http://support.stackmob.com/requests
        StackMobCommon.API_KEY = "b4958d0c-d955-4395-97e3-b48480ffc6c4";
        StackMobCommon.API_SECRET = "db4a3248-d849-4cc2-b2f7-2eec65b6ac70";
        StackMobCommon.USER_OBJECT_NAME = "";
        StackMobCommon.API_VERSION = 0;              
        //StackMobCommon.API_URL_FORMAT = "jinbo14.mob1.stackmob.com/api";
        mStackMob = StackMobCommon.getStackMobInstance();
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mButton1 = (Button) findViewById(R.id.button1);
        mTextView1 = (TextView) findViewById(R.id.textView1);
        
        mButton1.setOnClickListener(new ClickHandler());
    }
    
    private void threadAgnosticInfo(final TextView textView, final String txt) {
        runOnUiThread(new Runnable() {
            @Override public void run() {
                textView.setText(txt);
            }
        });
    }
}